package controle;

import java.io.IOException;


public class ArduinoIntervalController {
	private static int luminosidade[]= new int[256];
	private static int temperatura[] = new int[256];
	
	public static void inicializa(){
		for(int i = 0; i<256;i++){
			luminosidade[i] = 0;
			temperatura[i] = 0;
		}
	}
	
	public static int getLuzEmX(int x){
		return luminosidade[x];
	}
	
	public static int getTempEmX(int x){
		return temperatura[x];
	}
	
	public static void adicionaValoresLuz(int min, int max){
		int saida;
		int inicio = 0;
		int fim = 0;
		boolean flagInicio = false;
		
		for (int i = min; i<=max; i++){
			luminosidade[i]++;
			
			if (luminosidade[i] == 1){
				
				if(flagInicio == false){
					inicio = i;
					flagInicio = true;
				}
				
				fim = i;
			}
		}
		
		
		try {
			ArduinoConnect.getOut().write('L');
			ArduinoConnect.getOut().write('M');

			saida = inicio/100;
			
			ArduinoConnect.getOut().write(saida+48);
			saida = inicio/10;
			saida = saida%10;
			ArduinoConnect.getOut().write(saida+48);
			saida = inicio%10;
			ArduinoConnect.getOut().write(saida+48);
			
			saida = fim/100;
			ArduinoConnect.getOut().write(saida+48);
			saida = fim/10;
			saida = saida%10;
			ArduinoConnect.getOut().write(saida+48);
			saida = fim%10;
			ArduinoConnect.getOut().write(saida+48);
			
			ArduinoConnect.getOut().write('#');
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void adicionaValoresTemp(int min, int max){
		int saida;
		int inicio = 0;
		int fim = 0;
		boolean flagInicio = false;
		
		for (int i = min; i<=max; i++){
			temperatura[i]++;
			
			if (temperatura[i] == 1){
				
				if(flagInicio == false){
					inicio = i;
					flagInicio = true;
				}
				
				fim = i;
			}
		}
		
		
		try {
			ArduinoConnect.getOut().write('T');
			ArduinoConnect.getOut().write('M');

			
			
			saida = inicio/100;
			ArduinoConnect.getOut().write(saida+48);
			saida = inicio/10;
			saida = saida%10;
			ArduinoConnect.getOut().write(saida+48);
			saida = inicio%10;
			ArduinoConnect.getOut().write(saida+48);
			
			saida = fim/100;
			ArduinoConnect.getOut().write(saida+48);
			saida = fim/10;
			saida = saida%10;
			ArduinoConnect.getOut().write(saida+48);
			saida = fim%10;
			ArduinoConnect.getOut().write(saida+48);
			
			ArduinoConnect.getOut().write('#');
			
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void reduzValoresLuz(int min, int max){
		int saida;
		int inicio = 0;
		int fim = 0;
		boolean flagInicio = false;
		
		
		for (int i = min; i<=max; i++){
			luminosidade[i]--;
		
			if (luminosidade[i] == 0){
				
				if(flagInicio == false){
					inicio = i;
					flagInicio = true;
				
				}
				
				if (i == max){
					fim = i;
					flagInicio = false;
				
					
					try {
						ArduinoConnect.getOut().write('L');
						ArduinoConnect.getOut().write('D');

						System.out.println("INICIO = "+inicio);
						System.out.println("FIM = "+ fim);
						
						
						saida = inicio/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio%10;
						ArduinoConnect.getOut().write(saida+48);
						
						saida = fim/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim%10;
						ArduinoConnect.getOut().write(saida+48);
						
						ArduinoConnect.getOut().write('#');
						
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				}else if((luminosidade[i+1]) != 0){
					fim = i;
					try {
						ArduinoConnect.getOut().write('L');
						ArduinoConnect.getOut().write('D');

						System.out.println("INICIO = "+inicio);
						System.out.println("FIM = "+ fim);
						
						
						saida = inicio/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio%10;
						ArduinoConnect.getOut().write(saida+48);
						
						saida = fim/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim%10;
						ArduinoConnect.getOut().write(saida+48);
						
						ArduinoConnect.getOut().write('#');
						
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				if (luminosidade[i] < 0) luminosidade[i] = 0;
			}
			
			
		}
		
		
				
	
	
	public static void reduzValoresTemp(int min, int max){
		int saida;
		int inicio = 0;
		int fim = 0;
		boolean flagInicio = false;
		
		
		for (int i = min; i<=max; i++){
			temperatura[i]--;
		
			if (temperatura[i] == 0){
				
				if(flagInicio == false){
					inicio = i;
					flagInicio = true;
				
				}
				
				if (i == max){
					fim = i;
					flagInicio = false;
				
					
					try {
						ArduinoConnect.getOut().write('T');
						ArduinoConnect.getOut().write('D');


						
						
						saida = inicio/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio%10;
						ArduinoConnect.getOut().write(saida+48);
						
						saida = fim/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim%10;
						ArduinoConnect.getOut().write(saida+48);
						
						ArduinoConnect.getOut().write('#');
						
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}

				}else if((temperatura[i+1]) != 0){
					fim = i;
					try {
						ArduinoConnect.getOut().write('T');
						ArduinoConnect.getOut().write('D');



						saida = inicio/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = inicio%10;
						ArduinoConnect.getOut().write(saida+48);
						
						saida = fim/100;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim/10;
						saida = saida%10;
						ArduinoConnect.getOut().write(saida+48);
						saida = fim%10;
						ArduinoConnect.getOut().write(saida+48);
						
						ArduinoConnect.getOut().write('#');
						
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				if (temperatura[i] < 0) temperatura[i] = 0;
			}
			
			
		}

	
	
}
